package com.alan;

/**
 * 快速排序
 */
public class Kspx {
    private static int[] arr = {6,1,2,7,9,3,4,5,10,8};
    public static void main(String[] args) {
        sort(0, arr.length - 1);
        for(int num:arr){
            System.out.print(num + " ");
        }
    }

    public static void sort(int left, int right){
        if(left>=right)
            return;
        int i,j,temp,t;
        i = left;
        j = right;
        temp = arr[left];
        while(i!=j){
//先走右边
            while(arr[j]>=temp && i<j){
                j--;
            }
//再走左边
            while(arr[i]<=temp && i<j){
                i++;
            }
//交换数据
            t = arr[i];
            arr[i] = arr[j];
            arr[j] = t;
        }
//基准数归位
        arr[left] = arr[i];
        arr[i] = temp;

//递归左边
        sort(left, i-1);
//递归右边
        sort(j+1, right);
    }
}
